AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

function ENT:Initialize()
	self.BaseClass.BaseClass.Initialize(self)
	self:SetUseType(SIMPLE_USE)
end

function ENT:Use(ply, caller)
	local nwname = self.NWName
	local pname = self.PrintName
	
	if !ply:GetNWBool(nwname) then
		local canuse = self:GiveDrugEffects(ply)
		if canuse == nil || canuse then
			local tbl = self:GetTable()
			local realdrug = self.rdrug
			if realdrug then
				nwname = realdrug.NWName
			end
			timer.Create(ply:SteamID() .. nwname, self.DrugTime, 1, ClearDrugEffects, ply, tbl, realdrug)
			ply:ChatPrint(string.format("You took %s.", pname))
			
			self.Quantity = self.Quantity-1
			self.Entity:SetNWInt("Quantity", self.Quantity)
			if self.Quantity < 1 then
				self.Entity:Remove()
			elseif self.Quantity == 1 && self:GetModel() != "models/props_lab/jar01b.mdl" then
				self:SetModel("models/props_lab/jar01b.mdl")
				self:PhysicsInit(SOLID_VPHYSICS)
				self:GetPhysicsObject():Wake()
			end
		end
	end
end

local mdlsmallbottle = "models/props_lab/jar01b.mdl"
local mdlbigbottle = "models/props_lab/jar01a.mdl"
function ENT:StartTouch(ent)
	if ent.ClassName == self.ClassName then
		
		
		local loser = ent
		local winner = self
		if winner.Quantity >= 12 || loser.Quantity >= 12 then
			return
		end
		if ent.Quantity > self.Quantity then
			loser = self
			winner = ent
		end
		
		local losermodel = loser:GetModel()
		local modelissmall = losermodel == mdlsmallbottle
		local losercount = loser.Quantity
		local winnercount = winner.Quantity
		
		local total = losercount + winnercount
		if total > 12 then
			winner.Quantity = 12
			loser.Quantity = total-12
			loser.Entity:SetNWInt("Quantity", loser.Quantity)
			if loser.Quantity > 1 && modelissmall then
				loser:SetModel(mdlbigbottle)
				loser:PhysicsInit(SOLID_VPHYSICS)
				loser:GetPhysicsObject():Wake()
			elseif loser.Quantity == 1 && !modelissmall then
				loser:SetModel(mdlsmallbottle)
				loser:PhysicsInit(SOLID_VPHYSICS)
				loser:GetPhysicsObject():Wake()
			end
		else
			winner.Quantity = total
			loser.Quantity = 0
			loser:Remove()
		end
		winner.Entity:SetNWInt("Quantity", winner.Quantity)
		
		local cmodel = winner:GetModel()
		modelissmall = cmodel == mdlsmallbottle
		if winner.Quantity > 1 && modelissmall then
			winner:SetModel(mdlbigbottle)
			winner:PhysicsInit(SOLID_VPHYSICS)
			winner:GetPhysicsObject():Wake()
		elseif winner.Quantity == 1 && !modelissmall then
			winner:SetModel(mdlsmallbottle)
			winner:PhysicsInit(SOLID_VPHYSICS)
			winner:GetPhysicsObject():Wake()
		end
	end
end

function ENT:OnTakeDamage(dmg)
	if dmg:IsExplosionDamage() and self:GetSolid() == SOLID_NONE then return end
	
	self.BaseClass.BaseClass.OnTakeDamage(self, dmg)
end

// return false to cancel using the drug, return true or return nothing to use it
function ENT:GiveDrugEffects(ply)
	ply:SetNWBool(self.NWName, true)
end

function ENT:TakeDrugEffects(ply)

end

function ClearDrugEffects(ply, ctable, realdrug)
	if !ply then return end
	
	local adrug = ctable
	if realdrug then adrug = realdrug end
	
	adrug.TakeDrugEffects(adrug, ply)
	ply:SetNWBool(adrug.NWName, false)
	ply:ChatPrint(adrug.PrintName .. " wore off")
end

local function RemoveDrugEffects(ply, inflictor, attacker)
	local plid = ply:SteamID()
	for k, v in pairs(GetDrugTables()) do
		if ply:GetNWBool(v.NWName) then
			v:TakeDrugEffects(ply)
			ply:SetNWBool(v.NWName, false)
			timer.Remove(plid .. v.NWName)
		end
	end
end
hook.Add("PlayerDeath", "cleardrugsondeath", RemoveDrugEffects)

function ENT:CanRecycle()
	return true
end